<template>
  <div>
    <el-page-header content="添加用户" icon="" title="用户管理" />

    <el-form ref="userFormRef" :model="userForm" :rules="userFormRules" label-width="80px" status-icon class="userForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="userForm.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="userForm.password" type="password" />
      </el-form-item>
      <el-form-item label="性别" prop="gender">
        <el-select v-model="userForm.gender" class="m-2" placeholder="Select" style="width: 100%">
          <el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="userForm.role" class="m-2" placeholder="Select" style="width: 100%">
          <el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="个人简介" prop="introduction">
        <el-input v-model="userForm.introduction" type="textarea" />
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <Upload :avatar="userForm.avatar" @myChange="handleChange" />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="submitForm()">添加用户</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import upload from '@/util/upload'
import Upload from '@/components/upload/Upload.vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()
const userFormRef = ref()
const userForm = reactive({
  username: '',
  password: '',
  gender: 0,
  role: 2,
  introduction: '',
  avatar: '',
  file: null
})
const userFormRules = reactive({
  username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
  password: [{ required: true, message: '请选择密码', trigger: 'blur' }],
  gender: [{ required: true, message: '请选择性别', trigger: 'blur' }],
  role: [{ required: true, message: '请选择角色', trigger: 'blur' }],
  introduction: [{ required: true, message: '请输入简介', trigger: 'blur' }],
  avatar: [{ required: true, message: '请上传头像', trigger: 'blur' }]
})
// 性别选项
const options1 = [
  {
    label: '保密',
    value: 0
  },
  {
    label: '男',
    value: 1
  },
  {
    label: '女',
    value: 2
  }
]
// 角色选项
const options2 = [
  {
    label: '管理员',
    value: 1
  },
  {
    label: '编辑',
    value: 2
  }
]
// 选择完图片后的回调
const handleChange = (file) => {
  userForm.avatar = URL.createObjectURL(file)
  userForm.file = file
}
// 添加用户
const submitForm = () => {
  userFormRef.value.validate(async (valid) => {
    if (valid) {
      // 提交数据到后端
      // console.log(userForm)
      const res = await upload('/adminapi/user/add', userForm)
      ElMessage.success('添加成功！')
      router.push('/user-manage/userlist')
    }
  })
}
</script>

<style lang="scss" scoped>
.userForm {
  margin-top: 50px;
}
</style>
